package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.AddShopform;
import com.woniuxy.controller.form.DelShopByIdform;
import com.woniuxy.controller.form.FindAllshopsform;
import com.woniuxy.controller.form.FindShopByLikefrom;
import com.woniuxy.controller.info.Showpageinof;
import com.woniuxy.dao.PawnShopWhRelDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ShopCodeCopeExption;
import com.woniuxy.exception.deleShopIsNULL;
import com.woniuxy.model.PawnShop;
import com.woniuxy.dao.PawnShopDao;
import com.woniuxy.model.PawnShopWhRel;
import com.woniuxy.service.PawnShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.PawnShopWhRelService;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnShopServiceImpl extends ServiceImpl<PawnShopDao, PawnShop> implements PawnShopService {
    @Resource
    private PawnShopDao shopDao;
    @Resource
    private PawnShopWhRelDao shopWhRelDao;

    /*
     * 分页查询所有商店
     * */
    @Override
    public Result findpageshop(FindAllshopsform findAllshops) {

        Page<PawnShop> shopPage = baseMapper.selectPage(findAllshops, null);
        List<PawnShop> shopList = shopPage.getRecords();
        List<Showpageinof> showpageinofs = NewCopyBean.copyList(shopList, Showpageinof.class);//返回的真实对象集合

        ArrayList<Showpageinof> showpageinofs1 = new ArrayList<>();//创建一个集合来接收
        Page<Showpageinof> showpageinofPage = new Page<>();//返回的分页对象

        for (Showpageinof showpageinof : showpageinofs) {
            List<String> strings = shopDao.findshopAndWhNname(showpageinof.getShopCode());//联查方法查出仓库名字的集合
            String s = "";
            for (String string : strings) {
                s += string + ",";
            }
            showpageinof.setWhName(s);
            showpageinofs1.add(showpageinof);
        }

        showpageinofPage.setRecords(showpageinofs1);
        showpageinofPage.setSize(shopPage.getSize());
        showpageinofPage.setCurrent(shopPage.getCurrent());
        showpageinofPage.setTotal(shopPage.getTotal());
        showpageinofPage.setPages(shopPage.getPages());

        return Result.success(200, "查询成功", showpageinofPage);
    }

    /*
     * 根据模糊添加以及门店状态
     * */
    @Override
    public Result findShoppageBylike(FindShopByLikefrom findShopByLikefrom) {
        QueryWrapper<PawnShop> wq = new QueryWrapper<>();
        wq.like("shop_name", findShopByLikefrom.getLikeName())
                .or().like("contact", findShopByLikefrom.getLikeName())
                .or().like("phone_no", findShopByLikefrom.getLikeName());
        Page<PawnShop> shopPage = baseMapper.selectPage(findShopByLikefrom, wq);
        List<PawnShop> shopList = shopPage.getRecords();
        List<Showpageinof> showpageinofs = NewCopyBean.copyList(shopList, Showpageinof.class);//返回的真实对象集合
        ArrayList<Showpageinof> showpageinofs1 = new ArrayList<>();//创建一个集合来接收
        Page<Showpageinof> showpageinofPage = new Page<>();//返回的分页对象

        //如果门店状态条件为空时查询
        if (null == findShopByLikefrom.getShopStat() || findShopByLikefrom.getShopStat().equals("")) {
            for (Showpageinof showpageinof : showpageinofs) {
                List<String> strings = shopDao.findshopAndWhNname(showpageinof.getShopCode());
                String s="";
                for (String string : strings) {
                    s += string + ",";
                }
                showpageinof.setWhName(s);
                showpageinofs1.add(showpageinof);
            }
            showpageinofPage.setRecords(showpageinofs1);
            showpageinofPage.setTotal(shopPage.getTotal());
            showpageinofPage.setSize(shopPage.getSize());
            showpageinofPage.setCurrent(shopPage.getCurrent());
            showpageinofPage.setPages(shopPage.getPages());
            return Result.success(200, "查询成功", showpageinofPage);
        }else {//如果不为空时查询条件加上门店状态再来做分页查询
            QueryWrapper<PawnShop> wq1 = new QueryWrapper<>();
            wq1.eq("shop_stat",findShopByLikefrom.getShopStat());
            wq1.and(ww -> ww.
                    like("shop_name", findShopByLikefrom.getLikeName()).or().
                    like("contact", findShopByLikefrom.getLikeName()).or().
                    like("phone_no", findShopByLikefrom.getLikeName()).or());
            Page<PawnShop> shopPage2 = baseMapper.selectPage(findShopByLikefrom, wq1);

            List<Showpageinof> showlists = NewCopyBean.copyList(shopPage2.getRecords(), Showpageinof.class);//返回的真实对象集合
            ArrayList<Showpageinof> showlistinfo = new ArrayList<>();//创建一个集合来接收
            Page<Showpageinof> showpageinfo = new Page<>();//返回的分页对象
            for (Showpageinof showlist : showlists) {
                List<String> strings = shopDao.findshopAndWhNname(showlist.getShopCode());
                String s="";
                for (String string : strings) {
                    s += string + ",";
                }
                showlist.setWhName(s);
                showlistinfo.add(showlist);
            }
            showpageinfo.setRecords(showlistinfo);
            showpageinfo.setCurrent(shopPage2.getCurrent());
            showpageinfo.setSize(shopPage2.getSize());
            showpageinfo.setTotal(shopPage2.getTotal());
            showpageinfo.setPages(shopPage2.getPages());

            return Result.success(200, "查询成功", showpageinfo);
        }

    }

    /**
     * 删除商店
     * @param delShopByIdform
     * @return
     */

    @Override
    public Result deleshops(DelShopByIdform delShopByIdform) throws deleShopIsNULL {
        List<PawnShop> listshops = delShopByIdform.getListshops();
        if (ObjectUtils.isEmpty(listshops))throw new deleShopIsNULL(5000,"请勾选删除的店铺");
        for (PawnShop listshop : listshops) {
            baseMapper.delete(new QueryWrapper<PawnShop>().eq("shop_code",listshop.getShopCode()));
            shopWhRelDao.delete(new QueryWrapper<PawnShopWhRel>().eq("shop_code",
                    listshop.getShopCode()));
        }
        return Result.success(200,"删除成功",null);
    }

    @Override
    public Result addshop(AddShopform addShopform) throws DdException {
      if (!ObjectUtils.isEmpty(shopDao.selectOne(new QueryWrapper<PawnShop>()
              .eq("shop_name",addShopform.getShopName()))))
        throw new ShopCodeCopeExption(50002,"该店名已存在重新输入");
      shopDao.insert(new PawnShop(null,addShopform.getShopName(),addShopform.getContact(),addShopform.getPhoneNo()
      ,addShopform.getAddress(),addShopform.getShopStat()));
      if (!ObjectUtils.isEmpty(addShopform.getWhCode())){
          PawnShop shop_name = baseMapper.selectOne(new QueryWrapper<PawnShop>().eq("shop_name", addShopform.getShopName()));
          for (String integer : addShopform.getWhCode()) {
              shopWhRelDao.insert(new PawnShopWhRel(shop_name.getShopCode(),integer));
          }

      }
        return Result.success(200,"添加成功",null);
    }
}